package logic

import (
	"context"
	roleApi "mldong/api/role"
	dao "mldong/internal/modules/sys/dao"
	"mldong/internal/modules/sys/entity"
	"mldong/internal/modules/sys/enum"
	service "mldong/internal/modules/sys/service"
	"mldong/utility"
	"strings"

	"github.com/gogf/gf/v2/database/gdb"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/jinzhu/copier"
)

func init() {
	// 注册服务实现
	utility.Register(service.RoleServiceType, NewRoleLogic())
}

type RoleLogic struct {
	roleDao *dao.RoleDao
}

// 保存角色接口实现
func (logic *RoleLogic) Save(ctx context.Context, req *roleApi.SaveReq) (res bool, err error) {
	if req.AppCode == nil || strings.Trim(*req.AppCode, "") == "" {
		appCode := strings.ToLower(enum.MENU_APP_CODE_PLATFORM.Key) // 先赋值给变量
		req.AppCode = &appCode                                      // 再取地址
	}
	unique := utility.CheckUnique(logic.roleDao.Model(nil), "code", req.Code, nil, "唯一编码已存在，请检查code参数")
	if unique != nil {
		return false, unique
	}
	var role = entity.Role{}
	err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
		req.ID = nil
		// 将req对象赋值给role
		copier.Copy(&role, &req)
		// 插入sys_role表
		role, err = logic.roleDao.Insert(role, tx)
		if err != nil {
			return err
		}
		return nil
	})
	return role.ID != nil, err
}

// 修改角色接口实现
func (logic *RoleLogic) Update(ctx context.Context, req *roleApi.UpdateReq) (res bool, err error) {
	unique := utility.CheckUnique(logic.roleDao.Model(nil), "code", req.Code, req.ID, "唯一编码已存在，请检查code参数")
	if unique != nil {
		return false, unique
	}
	var role = entity.Role{}
	// 将req对象赋值给role
	copier.Copy(&role, &req)
	var count int64
	count, err = logic.roleDao.UpdateById(role, nil)
	if err != nil {
		return false, err
	}
	return count > 0, err
}

// 删除角色接口实现
func (logic *RoleLogic) Remove(ctx context.Context, ids []string) (res bool, _ error) {
	// 遍历ids
	// for _, id := range ids {
	// 	logic.roleDao.DeleteById(id)
	// }
	var count, err = logic.roleDao.DeleteByIds(ids)
	if err != nil {
		return false, err
	}
	if count == 0 {
		return false, err
	}
	return true, err
}

// 根据ID获取角色信息
func (logic *RoleLogic) Detail(ctx context.Context, id string) (res roleApi.RoleVO, err error) {
	var roleVO = roleApi.RoleVO{}
	var role entity.Role
	role, err = logic.roleDao.GetById(id)

	// 将role对象赋值给roleVO
	copier.Copy(&roleVO, &role)

	return roleVO, err
}

// 分页查询角色接口实现
func (logic *RoleLogic) Page(ctx context.Context, req *roleApi.PageReq) (res roleApi.RolePageVO, err error) {
	// 处理分页参数nil值
	utility.HandlePageParam(&req.PageParam)
	roles, total, err := logic.roleDao.Page(*req.PageNum, *req.PageSize, func(model *gdb.Model) *gdb.Model {
		model = utility.BuildQuery(req.PageParam, model)
		return model
	})
	var roleVOS = []roleApi.RoleVO{}
	copier.Copy(&roleVOS, &roles)
	rolePage := roleApi.RolePageVO{
		Rows: roleVOS,
	}
	rolePage.PageNum = *req.PageNum
	rolePage.PageSize = *req.PageSize
	rolePage.RecordCount = total
	// 计算 TotalPage
	if *req.PageSize > 0 {
		rolePage.TotalPage = (total + *req.PageSize - 1) / *req.PageSize
	} else {
		rolePage.TotalPage = 0 // 如果 PageSize 为 0，则 TotalPage 为 0
	}
	return rolePage, err
}
func NewRoleLogic() service.IRoleService {
	return &RoleLogic{
		roleDao: dao.NewRoleDao(),
	}
}
